package com.smart_campus.sys.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.smart_campus.sys.common.*;
import com.smart_campus.sys.domain.User;
import com.smart_campus.sys.domain.UserInfo;
import com.smart_campus.sys.service.UserInfoService;
import com.smart_campus.sys.vo.EstimateVo;
import com.smart_campus.sys.vo.UserEstimateVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.smart_campus.sys.mapper.UserEstimateMapper;
import com.smart_campus.sys.domain.UserEstimate;
import com.smart_campus.sys.service.UserEstimateService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
*@Author:Omega
*@Date:2020/6/18 15:06
*/
@Service
public class UserEstimateServiceImpl implements UserEstimateService{
    @Autowired
    UserInfoService userInfoService;

    @Resource
    private UserEstimateMapper userEstimateMapper;

    @Override
    public int deleteByPrimaryKey(Integer estimateid) {
        return userEstimateMapper.deleteByPrimaryKey(estimateid);
    }

    /**插入留言
     * @param record
     * @return
     */
    @Override
    public int insert(UserEstimate record) {
        return userEstimateMapper.insert(record);
    }

    @Override
    public int insertSelective(UserEstimate record) {
        return userEstimateMapper.insertSelective(record);
    }

    @Override
    public UserEstimate selectByPrimaryKey(Integer estimateid) {
        return userEstimateMapper.selectByPrimaryKey(estimateid);
    }

    @Override
    public int updateByPrimaryKeySelective(UserEstimate record) {
        return userEstimateMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(UserEstimate record) {
        return userEstimateMapper.updateByPrimaryKey(record);
    }

    /**
     * 插入留言回复
     * @param userEstimate
     * @return
     */
    @Override
    public UserEstimate insetUserEstimate(UserEstimate userEstimate) {
        if(userEstimate.getPid()==null){
            userEstimate.setPid(UserEntity.ESTIMATE_PID);
        }
        //得到评论人的个人信息
        Subject subject = SecurityUtils.getSubject();
        ActiveUser activeUser = (ActiveUser) subject.getPrincipal();
        User user = activeUser.getUser();
        userEstimate.setUsername(user.getUsername());
        userEstimate.setUserid(user.getUserId());
        UserInfo userInfo=userInfoService.selectByUserID(user.getUserId());
        userEstimate.setUserimg(userInfo.getFilt());
        userEstimate.setCreateTime(new Date());
        try {
            insertSelective(userEstimate);
            return userEstimate;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 通过当前登录用户的id查询全部评论
     * @param masterId
     * @return
     */
    @Override
    public List<UserEstimateVo> selectUserEstimate(Integer masterId,Integer PageNum) {
        UserEstimateVo EUV = new UserEstimateVo();
        EUV.setPageNum(PageNum);
        EUV.setMasterid(masterId);
        List<UserEstimate> ueList = new ArrayList<>();
        ueList=userEstimateMapper.selectUserEstimateList(EUV);
        List<UserEstimateVo> uEVoList = new ArrayList<>();
        //获取该用户的所有评论循环判断
        for (UserEstimate userEstimate : ueList) {
            //判断当前的用户的父ID是否一致
            if (userEstimate.getPid()==0){
                try {
                    UserEstimateVo userEstimateVo = new UserEstimateVo();
                    userEstimateVo.setMasterid(userEstimate.getMasterid());
                    userEstimateVo.setUserid(userEstimate.getUserid());
                    userEstimateVo.setPid(userEstimate.getPid());
                    userEstimateVo.setUsername(userEstimate.getUsername());
                    userEstimateVo.setEstimateid(userEstimate.getEstimateid());
                    userEstimateVo.setComment(userEstimate.getComment());
                    userEstimateVo.setEstimateid(userEstimate.getEstimateid());
                    userEstimateVo.setUserimg(userInfoService.selectByUserID(userEstimate.getUserid()).getFilt());
                    userEstimateVo.setCreateTime(userEstimate.getCreateTime());
                    UserEstimateVo UE2 = new UserEstimateVo();
                    UE2.setPid(userEstimate.getEstimateid());
                    List<UserEstimate> userEstimateList=userEstimateMapper.selectUserEstimateList(UE2);
                    for (UserEstimate estimate : userEstimateList) {
                        estimate.setUserimg(userInfoService.selectByUserID(estimate.getUserid()).getFilt());
                    }
                    userEstimateVo.setData(userEstimateList);
                    uEVoList.add(userEstimateVo);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return uEVoList;
    }

    @Override
    public DataGridView selall(EstimateVo estimateVo) {
        Page<Object> page = PageHelper.startPage(estimateVo.getPage(),estimateVo.getLimit());
        List<EstimateVo> list = userEstimateMapper.selall(estimateVo);
        return new DataGridView(page.getTotal(),list);
    }

    @Override
    public DataGridView fuzzyquery(UserEstimateVo estimateVo) {
        Page<Object> page = PageHelper.startPage(estimateVo.getPage(),estimateVo.getLimit());
        List<EstimateVo> list = userEstimateMapper.fuzzyquery(estimateVo);
        return new DataGridView(page.getTotal(),list);
    }

}
